package com.itheima.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.User;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/param")
public class ParamServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、单个数据获取
       /* String id = req.getParameter("id");
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        System.out.println(id+"-"+username+"-"+pwd);*/
       //2、获取map集合
       /* Map<String, String[]> map = req.getParameterMap();
        //2.1 创建一个用于接收数据的对象
        User u=new User();
        //2.2 beanutil
        try {
            BeanUtils.populate(u,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println(u);*/

        //3、读取流
        //BufferedReader reader = req.getReader();
      /*  String str="";
        while((str=reader.readLine())!=null){
            System.out.println(str);
        }*/

        String qs = req.getQueryString();
        System.out.println(qs);
        ObjectMapper obj=new ObjectMapper();
        User user = obj.readValue(qs, User.class);
        System.out.println(user);


        resp.getWriter().write("ok");
    }
}
